var id = getUrlParam('id');
//判断是否是用户下单没有地址跳过来的
    if (document.referrer.indexOf("checkout") != -1) {
        storage.setItem('referrer', document.referrer)
    }
    var area2 = new LArea();
    area2.init({
        'trigger': '#demo2',
        'valueTo': '#value2',
        'keys': {
            id: 'value',
            name: 'text'
        },
        'type': 2,
        'data': [provs_data, citys_data, dists_data]
    });

    function positions() {
        var geolocation = new BMap.Geolocation()
        geolocation.getCurrentPosition(function (res) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                getAddress(res.longitude, res.latitude)
            }
        })
    }

    console.log(storage.getItem('referrer'));
    $('#saveData').on('click', function () {
        var data = {};
        var realname = $(".realname").val();
        var mobile = $(".mobile").val();
        var area_select = $(".area_select").val();
        var area_select_arr = area_select.split(',')
        var detail = $(".detail").val();
        //获得提交的数据
        data["uaddres_name"] = realname;
        data['uaddres_detail'] = detail;
        data['uaddres_mobile'] = mobile;
        data['uaddres_province'] = area_select_arr[0];
        data['uaddres_city'] = area_select_arr[1];
        data['uaddres_county'] = area_select_arr[2];
        data['uaddres_id'] = id;
        data['is_default'] = $(".checkbox").is(':checked') ? 1 : 0;
        if (storage.getItem('referrer')) {
            var referrer = storage.getItem('referrer');
        } else {
            var referrer = '';
        }
        data['referrer'] = referrer;
        //表单验证
        if (realname == '') {
            taostr('请填写收件人', 1000)
            return;
        }
        if (!(/^1[34578]\d{9}$/.test(mobile))) {
            taostr('请填写正确手机号码', 1000)
            return;
        }
        if (area_select == '') {
            taostr('请选择区域', 1000)
            return;
        }
        if (detail == '') {
            taostr('请填写详情地址', 1000)
            return;
        }
        $.ajax({
            //contentType:'application/json',
            //headers:{token:window.localStorage.getItem('token')},
            //headers:{"token":window.localStorage.getItem('token'),"Content-Type":"text/plain;charset=UTF-8"},
            type: "post",
            url: Url + "address",
            data: data,
            dataType: 'json',
            crossDomain: true,
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {
                showDiv()
                //xhr.setRequestHeader("token",storageSe.getItem('token'));
                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                //成功提示跳转list
                if (storage.getItem('referrer')) {
                    location.href = storage.getItem('referrer')
                } else {
                    location.href = 'add_list.html';
                }

            },
            error: function (request, status, err) {
                failure(status)
            }
        })
    })

    function getAddress(longitude, latitude) {
        var address;
        //通过baiduMap API获取街道名称
        var map = new BMap.Map("allmap");
        var point = new BMap.Point(longitude - 0.06575728, latitude - 0.0773409);
        var gc = new BMap.Geocoder();

        gc.getLocation(point, function (rs) {
            console.log(rs)
            address = rs.addressComponents.province + "," + rs.addressComponents.city + "," + rs.addressComponents.district
            console.log(address)
            $("#demo2").val(address)
        });

    }

//修改

    getUaddrInfo(id);

    function getUaddrInfo(id) {
        $.ajax({
            type: "get",
            url: Url + 'getAddress/' + id,
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {
                showDiv()
                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                if (data) {
                    $('.realname').val(data['uaddres_name']);
                    $('.mobile').val(data['uaddres_mobile']);
                    $('.detail').val(data['uaddres_detail']);
                    $('.userAddrId').val(data['uaddres_id']);
                    $('.area_select').val(data['uaddres_province'] + ',' + data['uaddres_city'] + ',' + data['uaddres_county']);
                    if (data['is_default']) {
                        $(".checkbox").attr('checked', true);
                        $(".isDefault").css({
                            "transition": "0.5s",
                            "background": "#b4282d"
                        }).find("h6").css({"transition": "0.5s", "left": "0.4rem"})
                    } else {
                        $(".checkbox").attr('checked', false);
                        $(".isDefault").css({
                            "transition": "0.5s",
                            "background": "#ffffff"
                        }).find("h6").css({"transition": "0.5s", "left": "0rem"})
                    }
                }
            },
            error: function (request, status, err) {
                failure(status)
            }
        })
    }

    function isDefault(that) {
        if ($(that).is(":checked")) {
            $(".isDefault").css({"transition": "0.5s", "background": "#b4282d"}).find("h6").css({
                "transition": "0.5s",
                "left": "0.4rem"
            })
        } else {
            $(".isDefault").css({"transition": "0.5s", "background": "#ffffff"}).find("h6").css({
                "transition": "0.5s",
                "left": "0rem"
            })
        }
    }

